package com.practice.controller;

import com.alibaba.fastjson.JSON;
import com.practice.config.SpringConfig;
import com.practice.entity.Emp;
import com.practice.entity.MyReturn;
import com.practice.service.serviceImpl.EmpServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.stereotype.Controller;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author:高杰领
 * @date:2021/11/18 9:41
 * @description:Emp控制层
 * @version:1.0
 **/
//@WebServlet(name = "empController",urlPatterns = "/empController")
public class EmpController extends HttpServlet {
    MyReturn myReturn = new MyReturn();
    EmpServiceImpl empService;

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AnnotationConfigApplicationContext applicationContext = (AnnotationConfigApplicationContext) req.getServletContext().getAttribute("applicationContext");
        empService = applicationContext.getBean("empService", EmpServiceImpl.class);
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("application/json");
        List<Emp> empList = empService.selectAll();
        myReturn.setData(empList);
        myReturn.setCode(0);
        myReturn.setMsg("成功");
        String toJSONString = JSON.toJSONString(myReturn);
        resp.getWriter().write(toJSONString);
    }
}
